package org.halk.request;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.zhuhao.utils.httpclient.SpringHttpClientUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpMethod;

import java.net.URL;
import java.util.HashMap;
import java.util.List;

/**
 * 根据请求获取响应
 *
 * @Author haozhu7
 * @Date 2024/2/27 16:01
 **/
public class GetBody {

//    public static final String url = "http://cbd.brain.com/service/biims-clctdash/api/site/statistics/subject";
    public static final String url = "https://www.ccbd.tech/service/biims-console/api/download/bio/task/list";
    public static final String token = "Bearer 094be400-e523-45f5-a473-b258b3e025db";
    String resBodyStr = "";

    /**
     * 获取请求结果
     */
    @Before
    public void getResBody() {
        //读取配置文件中的传参
        URL paramUrl = this.getClass().getClassLoader().getResource("testFile/jsonParam.json");
        List<String> strings = FileUtil.readLines(paramUrl, "utf-8");
        String str = String.join("", strings);

        //请求头
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put("Authorization", token);

        //发送请求获取body
        resBodyStr = SpringHttpClientUtils.requestForObject(url, HttpMethod.POST, String.class, headMap, str);

//        resBodyStr = OkHttpUtil.postJsonParams(url, str, headMap);
//
//        resBodyStr = HttpRequest
//                .post(url)
//                .contentType("application/json")
//                .body(str)
//                .addHeaders(headMap)
//                .execute()
//                .body();
    }

    /**
     * 解析响应
     */
    @Test
    public void pareBody() {
        System.out.println("is json: " + JSONUtil.isJson(resBodyStr));

        JSON bodyJson = JSONUtil.parse(resBodyStr);
        List<String> list = (List<String>) bodyJson.getByPath("content.subjectId");
        System.out.println("list size : " + list.size());

        list.forEach(x -> System.out.print(x + ","));
        System.out.println();

        list.forEach(x -> System.out.println(x));
    }

}
